package com.situ.blog.controller;

import com.situ.blog.pojo.entity.Type;
import com.situ.blog.pojo.query.TypeQuery;
import com.situ.blog.service.ITypeService;
import com.situ.blog.service.impl.TypeServiceImpl;
import com.situ.blog.util.MyBeanUtils;
import com.situ.blog.util.PageInfo;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.List;

@WebServlet("/type")
public class TypeController extends BaseController{
    private ITypeService typeService = new TypeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TypeController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "selectAll":
                selectAll(req, resp);
                break;
        }
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TypeController.selectAll");
        List<Type> list = typeService.selectAll();
        toJSON(resp, Result.ok(list));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TypeServlet.selectByPage");
        TypeQuery typeQuery = MyBeanUtils.copyToBean(req, TypeQuery.class);
        PageInfo pageInfo = typeService.selectByPage(typeQuery);
        toJSON(resp, pageInfo);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TypeController.add");
        Type type = MyBeanUtils.copyToBean(req, Type.class);
        typeService.add(type);
        toJSON(resp, Result.ok("添加成功！"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TypeController.deleteById");
        String id = req.getParameter("id");
        typeService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功！"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TypeController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        typeService.deleteAll(ids);
        toJSON(resp, Result.ok("删除成功！"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TypeController.selectById");
        String id = req.getParameter("id");
        Type type = typeService.selectById(Integer.parseInt(id));
        toJSON(resp, Result.ok(type));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TypeController.update");
        Type type = MyBeanUtils.copyToBean(req, Type.class);
        typeService.update(type);
        toJSON(resp, Result.ok("修改成功！"));
    }
}
